package by.dkusch.aircompany.webapp.page.base.panels;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.Airport;
import by.dkusch.aircompany.model.tables.Country;
import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Flight;
import by.dkusch.aircompany.model.tables.Person;
import by.dkusch.aircompany.model.tables.Plane;
import by.dkusch.aircompany.model.tables.PlaneType;
import by.dkusch.aircompany.model.tables.Region;
import by.dkusch.aircompany.model.tables.Route;
import by.dkusch.aircompany.webapp.app.BasicAuthenticationSession;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.utils.GrantsResolver;
import by.dkusch.aircompany.webapp.page.flight.FlightPage;
import by.dkusch.aircompany.webapp.page.flight.RoutePage;
import by.dkusch.aircompany.webapp.page.main.HomePage;
import by.dkusch.aircompany.webapp.page.regional.AirportPage;
import by.dkusch.aircompany.webapp.page.regional.CountryPage;
import by.dkusch.aircompany.webapp.page.regional.RegionPage;
import by.dkusch.aircompany.webapp.page.staff.CrewPage;
import by.dkusch.aircompany.webapp.page.staff.PersonPage;
import by.dkusch.aircompany.webapp.page.tech.PlanePage;
import by.dkusch.aircompany.webapp.page.tech.PlaneTypePage;
import by.dkusch.aircompany.webapp.page.users.UserProfilePage;

import com.googlecode.wicket.kendo.ui.panel.KendoFeedbackPanel;
import com.googlecode.wicket.kendo.ui.widget.menu.Menu;
import com.googlecode.wicket.kendo.ui.widget.menu.item.IMenuItem;
import com.googlecode.wicket.kendo.ui.widget.menu.item.MenuItem;

public class MenuPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Component feedback;

	public Component getFeedback() {
		return feedback;
	}

	public MenuPanel(String id) {
		super(id);

		// Menu //
		this.add(new Menu("menu", newMenuItemList()));
		// FeedbackPanel //
		feedback = new KendoFeedbackPanel("feedback");
		this.add(feedback);

	}

	List<IMenuItem> newMenuItemList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();

		list.add(new MenuItem(new ResourceModel("p.menu.home")) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				setResponsePage(new HomePage());
			}

		});

		MenuItem menuPersonal = new MenuItem(new ResourceModel("p.menu.personal"), newPersonalMenuList());
		if (menuPersonal.getItems().size() > 0) {
			list.add(menuPersonal);
		}

		MenuItem menuFlight = new MenuItem(new ResourceModel("p.menu.flights"), newFlightsMenuList());
		if (menuFlight.getItems().size() > 0) {
			list.add(menuFlight);
		}

		MenuItem menuRegional = new MenuItem(new ResourceModel("p.menu.regional"), newRegionalMenuList());
		if (menuRegional.getItems().size() > 0) {
			list.add(menuRegional);
		}

		MenuItem menuTechnical = new MenuItem(new ResourceModel("p.menu.technical"), newTechnicalMenuList());
		if (menuTechnical.getItems().size() > 0) {
			list.add(menuTechnical);
		}

		MenuItem menuProfile = new MenuItem(new ResourceModel("p.menu.users"), newUsersMenuList());
		if (menuProfile.getItems().size() > 0) {
			list.add(menuProfile);
		}

		return list;
	}

	List<IMenuItem> newPersonalMenuList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();
		if (GrantsResolver.canDo(Person.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.person")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new PersonPage());
				}

			});
		}
		if (GrantsResolver.canDo(Crew.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.crew")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new CrewPage());
				}

			});
		}
		return list;
	}

	List<IMenuItem> newFlightsMenuList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();
		if (GrantsResolver.canDo(Flight.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.flight")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new FlightPage());
				}

			});
		}
		if (GrantsResolver.canDo(Route.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.route")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new RoutePage());
				}

			});
		}
		return list;
	}

	List<IMenuItem> newRegionalMenuList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();
		if (GrantsResolver.canDo(Airport.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.airport")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new AirportPage());
				}

			});
		}
		if (GrantsResolver.canDo(Region.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.region")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new RegionPage());
				}

			});
		}
		if (GrantsResolver.canDo(Country.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.country")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new CountryPage());
				}

			});
		}

		return list;
	}

	List<IMenuItem> newTechnicalMenuList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();
		if (GrantsResolver.canDo(Plane.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.plane")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new PlanePage());
				}

			});
		}
		if (GrantsResolver.canDo(PlaneType.class, ActionType.VIEW)) {
			list.add(new MenuItem(new ResourceModel("p.submenu.planetype")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new PlaneTypePage());
				}

			});
		}
		return list;
	}

	List<IMenuItem> newUsersMenuList() {
		List<IMenuItem> list = new ArrayList<IMenuItem>();
		if (BasicAuthenticationSession.get().isSignedIn()) {

			list.add(new MenuItem(new ResourceModel("p.submenu.userprofile")) {

				/**
			 * 
			 */
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					setResponsePage(new UserProfilePage());
				}

			});
		}
		return list;
	}

}
